package com.google.code.gaeom.util;

import java.lang.reflect.Constructor;

/**
 * @author Peter Murray <gaeom@pmurray.com>
 */
public class ObjectUtils
{
	@SuppressWarnings("unchecked")
	public static <T> T cast(Object o)
	{
		return (T) o;
	}

	public static Object newInstance(String typeName)
	{
		try
		{
			return newInstance(Class.forName(typeName));
		}
		catch (ClassNotFoundException e)
		{
			throw new IllegalStateException(e);
		}
	}

	public static <T> T newInstance(Class<T> clazz)
	{
		try
		{
			Constructor<T> constructor = clazz.getDeclaredConstructor();
			constructor.setAccessible(true);
			return constructor.newInstance();
		}
		catch (Exception e)
		{
			throw new IllegalStateException(e);
		}
	}

	public static boolean equals(Object o1, Object o2)
	{
		if (o1 == null)
		{
			return o2 == null;
		}
		else
		{
			return o1.equals(o2);
		}
	}

	public static void sleep(int i)
	{
		if (i > 0)
		{
			try
			{
				Thread.sleep(i);
			}
			catch (InterruptedException e)
			{
				throw new RuntimeException(e);
			}
		}
	}
}
